{# 
   the following KNOWL macro enables to tag certain locations in the
   HTML documents to reference to saved "Knowls". They contain all
   the information and form the stored knowledge. 
    * the knowl's title ends up in the page
    * the content is either revealed or immediately included, depends on
      what the macro does.
   The macro itself is only there for the presentation of the data via suitable
   HTML tags and associated javascript code. 
   
   Arguments: 
    * title: to overwrite the knowl's title, usually just leave it 
             to its default None.
#}
{% macro KNOWL(knowlid, title=none) -%}
{% with knowl = Knowl(knowlid, kwargs) -%}
{% if knowl.exists() -%}
  {% if title is none -%}
     <a class="knowl-title" knowl="{{ knowl.id }}" kwargs="{{ kwargs|urlencode }}">{{ knowl.title|safe }}</a>
  {%- else -%}
    {% if title|length > 0 -%}
     <a class="knowl-title" knowl="{{ knowl.id }}" kwargs="{{ kwargs|urlencode }}">{{ title|safe }}</a>
    {%- endif %}
  {%- endif %}

{%- else -%}
  {# knowl does not exist. wrong knowlid or opportunity to create one! #}
    {% if user_is_authenticated -%}
      <span class="knowl knowl-error">
      '{{knowlid}}'
      <a href="{{ url_for('knowledge.edit', ID=knowlid) }}">Create it</a>.
      </span> 
    {%- else -%}
      {% if title is none -%}
        <span class="knowl knowl-error">'{{knowlid}}'</span> 
      {%- else -%}
        {{title}}
      {%- endif %}
    {%- endif %}
{%- endif %}
{%- endwith %}
{%- endmacro %}

{% macro KNOWL_INC( knowlid, title=none, htag="h1") -%}
{% with knowl = Knowl(knowlid) -%}
{% if knowl.exists() -%}
  {% if title is none -%}
    <{{htag}}>{{knowl.title}}</{{htag}}>
  {%- else -%}
    <{{htag}}>{{title}}</{{htag}}>
  {%- endif %}
  {{knowl.content|render_knowl|safe }}
  {% if user_is_authenticated -%}
    <div class="knowl-inc-edit"><a href="{{ url_for('knowledge.edit', ID=knowlid) }}">edit '{{knowlid}}'</a></div>
  {%- endif %}

{%- else -%}
  {# knowl does not exist. wrong knowlid or opportunity to create one! #}
    {% if user_is_authenticated -%}
      <span class="knowl knowl-error">
      '{{knowlid}}'
      <a href="{{ url_for('knowledge.edit', ID=knowlid) }}">Create it</a>.
      </span> 
    {%- else -%}
       <span class="knowl knowl-error">'{{knowlid}}'</span> 
    {%- endif %}
{%- endif %}
{%- endwith %}
{%- endmacro %}

{# for inserting "normal" links to knowl pages #}
{% macro KNOWL_LINK(knowlid, title=none) -%}
{% with k = Knowl(knowlid) -%}
{% if k.exists() -%}
  <a class="knowl-link" href='{{ url_for("knowledge.show", ID=k.id) }}?{{kwargs|urlencode}}'>
  {% if title is none -%}{{ k.title }}{%- else -%}{{ title }}{%- endif -%}
 </a>
{%- else -%}
  {# knowl does not exist. wrong knowlid or opportunity to create one! #}
    {% if user_is_authenticated -%}
      <div class="knowl knowl-error">
       '{{knowlid}}'
       <a class="knowl-link" href="{{ url_for('knowledge.edit', ID=knowlid) }}">Create it</a>.
      </div> 
    {%- else -%}
      <span class="knowl knowl-error">'{{knowlid}}'</span> 
    {%- endif %}
{%- endif %}
{%- endwith %}
{%- endmacro %}

{# for the bar when editing or showing knowls #}
{% macro knowlbar() -%}
 <div class="knowl-bar">
   Actions: 
   <a title="as it will be used for transculsioins" href="{{ url_for('.render', ID=k.id) }}">render</a> 
   &middot;
   <a title="like as a wiki would" href="{{ url_for('.show', ID=k.id) }}">show</a>
   {% if user_is_authenticated %}
     &middot;
     <a href="{{ url_for('.edit', ID=k.id) }}">edit</a>
   {% endif %}
   {% if user_is_admin %}
     &middot;
     <a onclick='return confirm("Really delete Knowl {{ k.id }}? There is no way back!");'
            href="{{ url_for('.delete', ID=k.id) }}">delete</a>
   {% endif %}
   <span style="margin-left: 30px;">Navigate:</span>
   <a href="{{ url_for('.show', ID='wiki.index') }}">Knoiki</a>
   &middot;
   <a href="{{ url_for('.index') }}">Overview</a>
</div>
{%- endmacro %}

{# The defs for database queries #}
{% macro TEXT_DATA(filename, path) -%}
  {{ queryUploadDatabase(filename, path or request.path) }}
{%- endmacro %}
